package com.longmao.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longmao.eduservice.entity.EduSubject;
import com.longmao.eduservice.entity.excel.SubjectData;
import com.longmao.eduservice.service.EduSubjectService;
import com.longmao.servicebase.handler.LongMaoException;
import org.omg.CORBA.PUBLIC_MEMBER;

/**
 * @author longmao
 * @date 2022/1/26 10:20
 * Content:
 */
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    //因为SubjectExcelListener不能交给Spring进行管理，需要自己new，不能注入其他对象
    public EduSubjectService subjectService;

    public SubjectExcelListener(){}

    public SubjectExcelListener(EduSubjectService subjectService){
        this.subjectService = subjectService;
    }

    /**
     * 一行一行读取excel内容
     * @param subjectData
     * @param analysisContext
     */
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if(subjectData == null) {
            throw new LongMaoException(20001, "文件数据为空");
        }
        //先判断一级分类是否重复
        EduSubject existoneSubject = this.existOneSubject(subjectService, subjectData.getOneSubjectName());
        if(existoneSubject == null) {   //没有一级分类，进行添加
            existoneSubject = new EduSubject();
            existoneSubject.setParentId("0");
            existoneSubject.setTitle(subjectData.getOneSubjectName());  //一级分类名称
            subjectService.save(existoneSubject);
        }
        //获取一级分类的id值
        String pid = existoneSubject.getId();
        //添加二级分类
        EduSubject existtwoSubject = this.existTwoSubject(subjectService, subjectData.getOneSubjectName(), pid);
        if(existtwoSubject == null) {   //没有一级分类，进行添加
            existtwoSubject = new EduSubject();
            existtwoSubject.setParentId(pid);
            existtwoSubject.setTitle(subjectData.getTwoSubjectName());  //一级分类名称
            subjectService.save(existtwoSubject);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    /**
     * 判断一级分类不能重复添加
     * @param subjectService
     * @param pname
     * @return
     */
    private EduSubject existOneSubject(EduSubjectService subjectService, String pname){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", pname);
        wrapper.eq("parent_id", 0);
        EduSubject oneSubject = subjectService.getOne(wrapper);
        return oneSubject;
    }

    /**
     * 判断二级分类不能重复添加
     * @param subjectService
     * @param pname
     * @param pid
     * @return
     */
    private EduSubject existTwoSubject(EduSubjectService subjectService, String pname, String pid){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", pname);
        wrapper.eq("parent_id", pid);
        EduSubject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }
}
